package com.fowo.api.flow.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.flow.entity.ProcessTemplates;
import com.fowo.api.flow.model.dto.FlowEngineDTO;
import com.fowo.api.flow.model.vo.TemplateVo;
import com.fowo.api.flow.service.ProcessTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @fileName: ProcessTemplateController
 * @Description: 代码目的，作用，如何工作
 * @Author: pzh
 * @Date: 2023/4/6 0006 11:54
 */
@Tag(name = "审批模版")
@RequestMapping("/api/template")
@RestController
public class ProcessTemplateController {

    @Resource
    private ProcessTemplateService processTemplateService;

    @Operation(summary = "保存表单流程")
    @PostMapping("/save")
    public R saveForm(@Validated @RequestBody FlowEngineDTO flowEngineDTO) throws Exception {
        return R.run(() -> processTemplateService.jsonToBpmn(flowEngineDTO));
    }

    @Operation(summary = "表单流程详情")
    @GetMapping("/detail/{formId}")
    public R<ProcessTemplates> getFormTemplateById(@PathVariable("formId") String templateId) {
        return R.data(() -> processTemplateService.getFormTemplateById(templateId));
    }

    @Operation(summary = "修改表单流程")
    @PutMapping("/update/detail")
    public R updateFormDetail(@RequestBody ProcessTemplates template) {
        processTemplateService.updateFormDetail(template);
        return R.run(() -> processTemplateService.updateFormDetail(template));
    }

    @Operation(summary = "根据表单名称查询模版")
    @GetMapping("/query/table")
    public R <List<TemplateVo>>queryByTableName(@RequestParam(value = "tableName", required = false)String tableName) {
        return R.data(() -> processTemplateService.queryByTableName(tableName));
    }
}
